package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class TeamsActivity extends AppCompatActivity {
    ListView mteams;
    ArrayList<Team> teams = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mteams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TeamsActivity.this,TeamDetalActivity.class);
                Team team = teams.get(position);
                intent.putExtra("team",team);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        String jsonStr = getJsonStr();
        teams = getTeam(jsonStr);
    }

    private ArrayList<Team> getTeam(String jsonStr) {
        ArrayList<Team> teamArrayList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(jsonStr);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Team team = new Team();
                team.setId(jsonObject.getString("id"));
                team.setGroup(jsonObject.getString("group"));
                team.setName(jsonObject.getString("name"));
                team.setSheng(jsonObject.getInt("sheng"));
                team.setPing(jsonObject.getInt("ping"));
                team.setFu(jsonObject.getInt("fu"));
                team.setWingoal(jsonObject.getInt("wingoal"));
                team.setLosegoal(jsonObject.getInt("losegoal"));
                team.setTotalgoal(jsonObject.getInt("totalgoal"));
                team.setScore(jsonObject.getInt("score"));
                team.setPic(jsonObject.getString("pic"));
                team.setDetail(jsonObject.getString("detail"));

                teamArrayList.add(team);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return teamArrayList;
    }

    private String getJsonStr() {
        byte[] bytes = new byte[102400];
        InputStream inputStream = getResources().openRawResource(R.raw.teams);
        try {
            int len = inputStream.read(bytes);
            String Str = new String(bytes,0,len);
            return Str;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return teams.size();
    }

    @Override
    public Object getItem(int position) {
        return teams.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(TeamsActivity.this,R.layout.list_item_layout,null);

        TextView GroupId = view.findViewById(R.id.group_id);
        GroupId.setText(teams.get(position).getGroup());

        ImageView img = view.findViewById(R.id.Ig_nb);
        String pic = teams.get(position).getPic();
        img.setImageResource(getResources().getIdentifier(pic,"mipmap",getPackageName()));

        TextView Df = view.findViewById(R.id.df);
        Df.setText("比赛情况:"+teams.get(position).getName());

        TextView Spf = view.findViewById(R.id.spf);
        Spf.setText("得分情况：胜"+teams.get(position).getSheng()+"平"+teams.get(position).getPing()+"负"+teams.get(position).getFu());

        return view;
    }
}
    private void initView() {
        mteams = findViewById(R.id.lv_list);
        MyAdapter adapter = new MyAdapter();
        mteams.setAdapter(adapter);
    }
}
